using System.Linq;
using System.Web.Mvc;
using ExtendedMvc.Common.Diagnostics;
using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	public abstract class Input<T, TModel> : FormElement<T, TModel>
		where T : Input<T, TModel>
		where TModel : class {
		private object _value;

		protected object value { get { return _value; } }

		protected Input(IModelViewHelper<TModel> helper, string type, object value, string memberName, string id = null)
			: base(helper, HtmlTag.Input, id ?? memberName, memberName) {
			Check.Argument.IsNotValid(type, "type", t => HtmlInputType.Values.Contains(t));
			SetAttribute(HtmlAttribute.Type, type);
			Value(value);
		}

		public virtual T Value(object value) {
			_value = value;
			return (T)this;
		}

		protected override TagRenderMode TagRenderMode { get { return TagRenderMode.SelfClosing; } }

		protected override void SetContent() {
			if (value != null) SetAttribute(HtmlAttribute.Value, this.value);
			base.SetContent();
		}
	}
}
